package com.itheima.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @Name MybatisConfig
 * @Author BaiXin
 * @Date 2022-10-16 16:28
 */
@Configuration
public class MyBatisConfig {
    //sqlSessionFactory
    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource datasource){
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(datasource);
        sqlSessionFactoryBean.setTypeAliasesPackage("com.itheima.pojo");
        //mapper接口和mapper.xml编译后在同一个目录下，可以不设置
        // sqlSessionFactoryBean.setMapperLocations();

        //设置mybatis的全局配置文件
        //sqlSessionFactoryBean.setConfigLocation();

        //设置插件
        //sqlSessionFactoryBean.setPlugins();

        return sqlSessionFactoryBean;
    }


    //Mapper代理
    //spring会扫描所有的mapper接口，生成代理对象，注入到spring容器中，默认的beanId就是类名首字母小写
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.itheima.mapper");
        return mapperScannerConfigurer;
    }

}
